package cc.admin.modules.sys.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.modules.sys.entity.SysLicence;
import cc.admin.modules.sys.service.ISysLicenceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * 许可管理
 * Sys licence controller
 *
 * @author 赵森涛
 * @date 2023.02.15 16:32:33
 * @description description
 */
@Slf4j
@Api(tags = "许可管理")
@RestController
@RequestMapping("/sys/licence")
public class SysLicenceController extends BaseController<SysLicence, ISysLicenceService> {

    @Autowired
    private ISysLicenceService licenceService;


    /**
     * 分页列表查询
     *
     * @param licenceName the licence name
     * @param tenantId   the company id
     * @param pageNo      the page no
     * @param pageSize    the page size
     * @param req         the req
     * @return result result
     */
    @AutoLog(value = "许可管理-分页列表查询")
    @ApiOperation(value = "许可管理-分页列表查询", notes = "许可管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            @RequestParam(name = "licenceName", required = false) String licenceName,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        QueryWrapper<SysLicence> queryWrapper =new QueryWrapper();
        if (!StringUtils.isEmpty(licenceName)){
            queryWrapper.like("licence_name", licenceName);
        }
        queryWrapper.orderByDesc("create_time");
        Page<SysLicence> page = new Page<>(pageNo, pageSize);
        IPage<SysLicence> pageList = licenceService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param syslicence the syslicence
     * @return result result
     */
    @AutoLog(value = "许可管理-添加")
    @ApiOperation(value = "许可管理-添加", notes = "许可管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysLicence syslicence) {
        licenceService.save(syslicence);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param syslicence the syslicence
     * @return result result
     */
    @AutoLog(value = "许可管理-编辑")
    @ApiOperation(value = "许可管理-编辑", notes = "许可管理-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SysLicence syslicence) {
        licenceService.updateById(syslicence);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id the id
     * @return result result
     */
    @AutoLog(value = "许可管理-通过id删除")
    @ApiOperation(value = "许可管理-通过id删除", notes = "许可管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        licenceService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids the ids
     * @return result result
     */
    @AutoLog(value = "许可管理-批量删除")
    @ApiOperation(value = "许可管理-批量删除", notes = "许可管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.licenceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id the id
     * @return result result
     */
    @AutoLog(value = "许可管理-通过id查询")
    @ApiOperation(value = "许可管理-通过id查询", notes = "许可管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SysLicence syslicence = licenceService.getById(id);
        return Result.ok(syslicence);
    }

    /**
     * 导出excel
     *
     * @param request    the request
     * @param sysLicence the sys licence
     * @return the model and view
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysLicence sysLicence) {
        return super.exportXls(request, sysLicence, SysLicence.class, "许可管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request  the request
     * @param response the response
     * @return result result
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysLicence.class);
    }


    /**
     * 船舶下拉
     * @return
     */
    @RequestMapping(value = "/getShip", method = RequestMethod.GET)
    public Result<?> getShip(){
        return Result.ok(licenceService.getShip());
    }



}
